#!/usr/bin/env python
# coding: utf-8

import multiprocessing

class FloatChannel(object):
    def __init__(self, maxsize):
        self.buffer = multiprocessing.RawArray('d', maxsize) #共享内存中
        self.buffer = multiprocessing.Array('d', maxsize) #共享内存中
        self.buffer_len = multiprocessing.Value('i')  #共享内存中
        self.empty = multiprocessing.Semaphore(1) #共享内存中
        self.full = multiprocessing.Semaphore(0) #共享内存中
    def send(self, values):
        print("send:", type(self.buffer_len), id(self),self.buffer_len)
        self.empty.acquire()
        nitems = len(values)
        self.buffer_len.value = nitems
        self.buffer[:nitems] = values
        self.full.release()

    def recv(self):
        self.full.acquire()
        print("recv:", type(self.buffer_len), id(self), self.buffer_len.value)
        values = self.buffer[:self.buffer_len.value]
        self.empty.release()
        return values

def consume_test(count, ch):
    for i in range(count):
        ch.recv()

def produce_test(count, values, ch):
    for i in range(count):
        ch.send(values)

if __name__ == '__main__':
    ch = FloatChannel(100000)
    p = multiprocessing.Process(target=consume_test, args=(1000, ch))
    p.start()
    values = [float(x) for x in range(100000)]
    produce_test(1000, values, ch)
    print("Done")
    p.join()